package com.blog.dxxr.blogfront.Controller;

import com.blog.dxxr.blogfront.Common.Domain.ReturnObject;
import com.blog.dxxr.blogfront.Domain.Article;
import com.blog.dxxr.blogfront.Domain.ArticleSecondCategory;
import com.blog.dxxr.blogfront.Domain.ArticleTag;
import com.blog.dxxr.blogfront.Domain.ArticleWithBLOBs;
import com.blog.dxxr.blogfront.Service.ArticleCategoryService;
import com.blog.dxxr.blogfront.Service.ArticleCommentService;
import com.blog.dxxr.blogfront.Service.ArticleService;
import com.blog.dxxr.blogfront.Service.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexPageController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleTagService articleTagService;
    @Autowired
    private ArticleCategoryService articleCategoryService;
    @Autowired
    private ArticleCommentService articleCommentService;

    //通过页数异步查询界面文章
    @RequestMapping("/index/queryArticleListByPageNo.do")
    public @ResponseBody Object queryArticleListByPageNo(int pageNo,String categoryId,String tagId){
        List<Article> articleList = null;
        int total = 0;
        if (tagId == null){
            Map<String,Object> map = new HashMap<>();
            map.put("pageNo",(pageNo - 1) * 5);
            map.put("categoryId",categoryId);
            articleList = articleService.queryArticleListIndex(map);
            total = articleService.queryArticleTotalIndex(map);
        }
        else {
            String[] tagIds = articleTagService.queryArticleIdArrayByTagId(tagId);
            articleList = articleService.queryArticleListIndexByArticleIds(tagIds);
            total = articleService.queryArticleListTotalIndexByArticleIds(tagIds);
        }
        for (Article article: articleList) {
            int commentTotal = articleCommentService.selectCommentTotalByArticleId(article.getId());
            article.setCommentTotal(commentTotal);
        }
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("articleList",articleList);
        retMap.put("total",total);
        return retMap;
    }

    //异步查询所有标签
    @RequestMapping("/index/queryAllArticleList.do")
    public @ResponseBody Object queryAllArticleList(){
        List<ArticleTag> articleTagList = articleTagService.queryAllTags();
        List<ArticleSecondCategory> secondCategoryList = articleCategoryService.querySecondCategoryList();
        Map<String,Object> map = new HashMap<>();
        map.put("articleTagList",articleTagList);
        map.put("secondCategoryList",secondCategoryList);
        return map;
    }

//    //异步通过标签查询文章
//    @RequestMapping("/index/queryAllArticleListByTag.do")
//    public @ResponseBody Object queryAllArticleListByTag(String tagId){
//        String[] tagIds = articleTagService.queryArticleIdArrayByTagId(tagId);
//        List<Article> articleList = articleService.queryArticleListIndexByArticleIds(tagIds);
//        int total = articleService.queryArticleListTotalIndexByArticleIds(tagIds);
//        Map<String,Object> map = new HashMap<>();
//        map.put("articleList",articleList);
//        map.put("total",total);
//        return map;
//    }

    //异步查询最近的文章
    @RequestMapping("/index/queryArticleRecent.do")
    public @ResponseBody Object queryArticleRecent(){
        List<Article> articleList = articleService.queryArticleListRecent();
        return articleList;
    }


}
